package com.turnengine.client.local.location.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.location.bean.CoordinateGridSerializer;
import com.turnengine.client.local.location.bean.ICoordinateGrid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Get Coordinate Grids Data Serializer.
 */
public class GetCoordinateGridsDataSerializer extends AbstractCommandDataSerializer<List<ICoordinateGrid>> implements IGetCoordinateGridsDataSerializer {

	/**
	 * Creates a new Get Coordinate Grids Data Serializer.
	 */
	public GetCoordinateGridsDataSerializer() {
		super(GetCoordinateGrids.COMMAND_ID);
	}

	@Override
	public List<ICoordinateGrid> readReturnValue(IDataReader reader) throws IOException {
		List<ICoordinateGrid> value = reader.readObjectCollection(new ArrayList<ICoordinateGrid>(), true, false, new CoordinateGridSerializer());
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, List<ICoordinateGrid> value) throws IOException {
		writer.writeObjectCollection(value, true, false, new CoordinateGridSerializer());
	}

	@Override
	public IExecutableCommand<List<ICoordinateGrid>> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();

		// Create object
		GetCoordinateGrids object = new GetCoordinateGrids();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<List<ICoordinateGrid>> argument) throws IOException {
		GetCoordinateGrids object = (GetCoordinateGrids) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
	}

	@Override
	public IExecutableCommandResponse<List<ICoordinateGrid>> newResponse() {
		return new GetCoordinateGridsResponse();
	}
}
